import json
import os
import glob

# Repository list from the original code
REPO_List = [
    "account_os_account",
    "arkui_ace_engine",
    "build",
    "communication_wifi",
    "developtools_ace_ets2bundle",
    "multimedia_audio_framework",
    "web_webview",
    "xts_acts",
    "kernel_linux_5.10"
]

def count_json_objects_in_file(filepath):
    """Count the number of JSON objects in a JSONL file"""
    try:
        count = 0
        with open(filepath, 'r', encoding='utf-8') as f:
            for line in f:
                if line.strip():  # Skip empty lines
                    json.loads(line.strip())
                    count += 1
        return count
    except FileNotFoundError:
        return f"File not found: {filepath}"
    except Exception as e:
        return f"Error reading {filepath}: {str(e)}"

# Count for each repository file
print("=== RQ1   Repository-specific files ===")
total_repo_files = 0
for repo in REPO_List:
    OWNER = "openharmony"
    REPO_OUTPUT_FILE = f"./RQ1/{repo}/{OWNER}_{repo}_check_data.jsonl"
    count = count_json_objects_in_file(REPO_OUTPUT_FILE)
    print(f"{REPO_OUTPUT_FILE}: {count}")
    if isinstance(count, int):
        total_repo_files += count

print(f"\nTotal objects in all repo files: {total_repo_files}")

# Count for aggregate files
print("\n=== Aggregate files ===")
aggregate_files = [
    "./RQ1/all_repos_check_data.jsonl",
    "./RQ1/train_data.jsonl",
    "./RQ1/test_data.jsonl"
]

for filepath in aggregate_files:
    count = count_json_objects_in_file(filepath)
    print(f"{filepath}: {count}")


print("=== RQ2 Repository-specific files ===")
total_repo_files = 0
for repo in REPO_List:
    OWNER = "openharmony"
    REPO_OUTPUT_FILE = f"./RQ2/{repo}/{OWNER}_{repo}_check_data.jsonl"
    count = count_json_objects_in_file(REPO_OUTPUT_FILE)
    print(f"{REPO_OUTPUT_FILE}: {count}")
    if isinstance(count, int):
        total_repo_files += count

print(f"\nTotal objects in all repo files: {total_repo_files}")

# Count for aggregate files
print("\n=== Aggregate files ===")
aggregate_files = [
    "./RQ2/all_repos_check_data.jsonl",
    "./RQ2/train_data.jsonl",
    "./RQ2/test_data.jsonl"
]

for filepath in aggregate_files:
    count = count_json_objects_in_file(filepath)
    print(f"{filepath}: {count}")
